package com.nuhu.controller;

import com.nuhu.pojo.User;
import com.nuhu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/register")
    public String registerUser(@RequestBody User user) {
        userService.registerUser(user);
        return "User registered successfully";
    }
    @PostMapping("/login")
    public Map<String, Object> loginUser(@RequestBody User user) {
        Map<String, Object> response = new HashMap<>();
        if (userService.loginUser(user)) {
            response.put("success", true);
            response.put("message", "Login successful");
        } else {
            response.put("success", false);
            response.put("message", "Login failed");
        }
        return response;
    }
    @GetMapping("/getUserName")
    public String getUserNameByPhoneNumber(@RequestParam("phoneNumber") String phoneNumber) {
        return userService.getUserNameByPhoneNumber(phoneNumber);
    }
    @GetMapping("/getIdCardNumber")
    public String getIdCardNumberByPhoneNumber(@RequestParam("phoneNumber") String phoneNumber) {
        return userService.getIdCardNumberByPhoneNumber(phoneNumber);
    }
    @GetMapping("/getPackageNameByPhoneNumber")
    public String getPackageNameByPhoneNumber(@RequestParam String phoneNumber) {
        int packageId = userService.getPackageIdByPhoneNumber(phoneNumber);
        return userService.getPackageNameById(packageId);
    }

    @GetMapping("/getPackageDataLimitByPhoneNumber")
    public BigDecimal getPackageDataLimitByPhoneNumber(@RequestParam String phoneNumber) {
        int packageId = userService.getPackageIdByPhoneNumber(phoneNumber);
        return userService.getPackageDataLimitById(packageId);
    }
    @PutMapping("/balance")//龚邓坤增加
    public void rechargeBalance(@RequestBody Map<String, Object> payload) {
        String phoneNumber = (String) payload.get("phoneNumber");
        BigDecimal amount = new BigDecimal(payload.get("amount").toString());
        String paymentMethod = "支付宝";
        Date paymentDate = new Date();
        String Payment_Reason="话费充值";
        int packageId = 0;
        int customerServiceId=0 ;
        int repairmanId =0;
        if (userService.checkPhoneNumberExists(phoneNumber)) {
            //userService.insertCompanyBill(paymentMethod, amount, paymentDate, Payment_Reason,  packageId, phoneNumber, customerServiceId, repairmanId);
            userService.rechargeBalance(phoneNumber, amount);

        } else {
            // 处理手机号不存在的情况
            throw new RuntimeException("手机号不存在");
        }
    }
    @PutMapping("/Data_Balance")//龚邓坤增加
    public void rechargeData_Balance(@RequestBody Map<String, Object> payload) {
        String phoneNumber = (String) payload.get("phoneNumber");
        BigDecimal Data_Balance = new BigDecimal(payload.get("Data_Balance").toString());
        if (userService.checkPhoneNumberExists(phoneNumber)) {
            userService.rechargeData_Balance(phoneNumber, Data_Balance);
        } else {
            // 处理手机号不存在的情况
            throw new RuntimeException("手机号不存在");
        }
    }

    @PutMapping("/package")//龚邓坤增加
    public void updateDataBalanceAndPackageId(@RequestBody Map<String, Object> payload) {
        String phoneNumber = (String) payload.get("phoneNumber");
        BigDecimal newDataBalance = new BigDecimal(payload.get("Data_Balance").toString());
        int newPackageId = (int) payload.get("Package_ID");
        userService.updateDataBalanceAndPackageId(phoneNumber, newDataBalance, newPackageId);
    }
    @PutMapping("/phone")
    public void phoneUser(@RequestBody Map<String, Object> payload) {
        int newPackageId = (int) payload.get("Package_ID");
        String phoneNumber = (String) payload.get("phoneNumber");
        String userName = (String) payload.get("User_Name");
        String idCardNumber = (String) payload.get("ID_Card_Number");
        userService.phoneUser(newPackageId, phoneNumber, userName, idCardNumber);
    }
}
